//
//  AAJTTItemListViewController.swift
//  JourneyThroughTime
//
//  Created by iOS-Czz on 2023/11/4.
//

import UIKit
import ProgressHUD
class AAJTTItemListViewController: AJTTBaseViewController {
    let scrollView = UIScrollView()
    let cardSize = CGSize(width: Screen_Width - 30, height: 300)
    let numberOfCards = 3
    // 声明一个可重用视图池数组
    var reusableCardViews = [AJTTItemListView]()

    lazy var viewModel: AJTTItemViewModel = {
        return AJTTItemViewModel()
    }()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        scrollView.frame = view.bounds
        view.addSubview(scrollView)
        
    }
    func loadData(){
        self.viewModel.loadData { [self] models in
            addCardViews()
        }
    }

   @objc func tapCardView(_ sender: UITapGestureRecognizer) {
        guard let cardView = sender.view else { return }
        let index = cardView.tag
       if index == self.viewModel.dataSource.count - 1 {
           gotoAddItemAction()
       }else{
           let detailsVC = AJTTItemDetailsController()
           detailsVC.model = self.viewModel.dataSource[index]
           self.navigationController?.pushViewController(detailsVC, animated: true)
       }
        // 执行相关操作
    }

    func createCardView(frame: CGRect, index: Int) -> UIView {
        let cardView:AJTTItemListView = Bundle.main.loadNibNamed("AJTTItemListView", owner: self, options: nil)?.first as! AJTTItemListView
        cardView.frame = frame
        cardView.backgroundColor = index % 2 == 0 ? #colorLiteral(red: 0.4028627872, green: 0.6332114339, blue: 0.9382972121, alpha: 1) : #colorLiteral(red: 0.9011769891, green: 0.8042932153, blue: 0.396549046, alpha: 1)
        cardView.layer.cornerRadius = 20.0
        cardView.layer.shadowColor = UIColor.black.cgColor
        cardView.layer.shadowOpacity = 0.5
        cardView.layer.shadowOffset = CGSize(width: 2, height: 2)
        cardView.layer.shadowRadius = 4
        cardView.tag = index

        let tapAction = UITapGestureRecognizer(target: self, action: #selector(tapCardView(_:)))
        cardView.addGestureRecognizer(tapAction)
        cardView.isUserInteractionEnabled = true

        return cardView
    }
    func addCardViews() {
        let model = AJTTItemModel()
        model.name = "Add new Item"
        self.viewModel.dataSource.append(model)
        var yOffset: CGFloat = 200.0
        for index in 0..<viewModel.dataSource.count {
            var cardView: AJTTItemListView
            
            if index < reusableCardViews.count {
                // 如果可重用池中有可用的视图，则从可重用池中获取
                cardView = reusableCardViews[index]
            } else {
                // 如果可重用池中没有足够的视图，则创建新的视图并添加到可重用池中
                cardView = createCardView(frame: .zero, index: index) as! AJTTItemListView
                reusableCardViews.append(cardView)
            }
            let cardFrame = CGRect(x: 15, y: yOffset, width: cardSize.width, height: cardSize.height)
            cardView.frame = cardFrame
            let model = self.viewModel.dataSource[index]
            cardView.nameLabel.text = model.name
            if index == self.viewModel.dataSource.count - 1 {
                cardView.nextButton.setImage(UIImage(named: "item_add"), for: .normal)
            }
            scrollView.addSubview(cardView)
            yOffset += 100
        }
    }
    @objc func gotoAddItemAction(){
        self.navigationController?.pushViewController(AJTTItemAddStepOneController(), animated: true)
    }
}
